package Server.Jsmj.RGCP;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.*;
import Server.Agent.GameAgent;
import Server.Hnmj.HZMJ.MajTable;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;

public class RGCPTableMgr {
	static RGCPTableMgr __instance = new RGCPTableMgr();

	public static RGCPTableMgr instance() {
		return __instance;
	}

	Long m_LastDealTime;
	Long m_LastUpdateTime;

	static Map<Integer, RGCPTable> m_mapTable = new HashMap<>();// 房间号 . 房间
	Map<Integer, Long> m_mapHeartTime = new HashMap<>();// 心跳时间

	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}

	static RGCPTable FindTable(int _Uid) {
		RGCPTable it = (RGCPTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void Handle_onHeart(int _Uid) {

	}

	void Handle_onConnect(int _Uid) {
	}

	void Handle_onJoin(int _Uid) {

	}

	static void Handle_onLeave(GameAgent agent, String[] vecArg) {

		Integer _Uid = agent.getUid();
		RGCPTable pTable = (RGCPTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true);
			
			
			QpGameMgr.getInstance().Handle_OnUserRoom(agent,new JSONObject());			
		}
	}

	void Handle_onDismissRoom(int _Uid) {

		RGCPTable tbl = FindTable(_Uid);
		if (tbl != null) {
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			tbl.DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(tbl);
		}

	}
	public static RGCPTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			int jushu = 0;
			String strRoomDesc = "";
			String payway = "FZPAY";
			int fdFen = 0;
			int diFen = 0;

			boolean f_shuanglong = false;
			boolean f_sanlao = false;
			boolean f_haidi = false;
			int byCostGem = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("3JU")) {
					jushu = 3;
					strRoomDesc += "3局 ";
					byCostGem = 3;
				}
				if (param.equalsIgnoreCase("6JU")) {
					jushu = 6;
					strRoomDesc += "6局 ";
					byCostGem = 6;
				}
				if (param.equalsIgnoreCase("12JU")) {
					jushu = 12;
					strRoomDesc += "12局 ";
					byCostGem = 12;
				}
			}
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("FZPAY")) {
					payway = "FZPAY";
					strRoomDesc += "房主支付 ";
					byCostGem *= 3;
				}
				if (param.equalsIgnoreCase("AAPAY")) {
					payway = "AAPAY";
					strRoomDesc += "AA支付 ";
				}
				if (param.equalsIgnoreCase("WINPAY")) {
					payway = "WINPAY";
					strRoomDesc += "赢家支付 ";
					byCostGem *= 3;
				}
				if (param.equalsIgnoreCase("CLUBPAY")) {
					payway = "CLUBPAY";
					strRoomDesc += "圈主支付 ";
					byCostGem *= 3;
				}
			}

			boolean is_xier = true;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("dai")) {
					is_xier = true;
					strRoomDesc += "带喜 ";
				}
				if (param.equalsIgnoreCase("budai")) {
					is_xier = false;
					strRoomDesc += "不带喜 ";
				}
			}

			int maiZhuang = 0;

			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("0BEI")) {
					maiZhuang = 0;
					strRoomDesc += "无倍数 ";
				}
				if (param.equalsIgnoreCase("1BEI")) {
					maiZhuang = 1;
					strRoomDesc += "一二三 ";
				}
				if (param.equalsIgnoreCase("2BEI")) {
					maiZhuang = 2;
					strRoomDesc += "二三四 ";
				}
				if (param.equalsIgnoreCase("3BEI")) {
					maiZhuang = 3;
					strRoomDesc += "三四五 ";
				}
			}

			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("shuanglong")) {
					f_shuanglong = true;
					strRoomDesc += "双龙会 ";
				}
			}

			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("sanlao")) {
					f_sanlao = true;
					strRoomDesc += "三老会面 ";
				}
			}
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("haidi")) {
					f_haidi = true;
					strRoomDesc += "海底捞月 ";
				}
			}
			int maxCost = byCostGem;
			if(payway.equals("CLUBPAY")){
				maxCost = 0;
			}
		
			boolean bIsDaKun = true;
			if (user.getGem(0) >= maxCost) {
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					factor = rand_room;
					RGCPTable room = (RGCPTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new RGCPTable();
						room.SetParam(strParam);
						room.SetDesc(strRoomDesc);
						room.maiZhuang = maiZhuang;
						room.is_xier = is_xier;
						room.m_bIsDaKun = bIsDaKun;
						room.m_diFen = diFen;
						room.m_fdFen = fdFen;
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_payWay = payway;
						room.m_byMaxRound = (int) jushu;
						room.m_Creator = _Uid;
						room.fangzhu = _Uid;

						room.m_MajLogic = new RGCPLogic();
						room.m_MajPaiQiang = new MajPaiQiang(is_xier);

						// TODO ��������
						room.f_shuanglong = f_shuanglong;
						room.f_haidilaoyue = f_haidi;
						room.f_sanlaohuimian = f_sanlao;
						room.Init(Constants.INIT_TOTAL);
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);
						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	void OnDeal(float dt) {
		Long tnow_ms = getCurrentTimeMillis();
		if (m_LastDealTime == 0) {
			m_LastDealTime = tnow_ms;
		}

		if (tnow_ms - m_LastDealTime >= 1000) {
			m_LastDealTime = tnow_ms;

		}
	}

	private Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * �İ��齫ָ������
	 * 
	 * @param agent
	 * @param root
	 */
	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		Integer lUid = agent.getUid();

		SHMUser shmUser = dthall.getInstance().getUserInfo(lUid);
		if (shmUser == null) {
			return;
		}
		if (vecArg2.length > 0) {
			String subcmd = vecArg2[0];
			if (subcmd == "connect") {

			}
			if (subcmd.equals("join")) {

			} else if (subcmd.equals("leave")) {
				Handle_onLeave(agent,vecArg2);
			} else if (subcmd.equals("dismissroom")) {

			} else {
				IRoom iterUser = FindTable(lUid);
				if (iterUser == null) {
					return;
				}
				RGCPTable tbl = (RGCPTable) (iterUser);
				if (tbl != null) {
					if (subcmd.equals("ready")) {
						tbl.Handle_onReady(lUid, vecArg2);
					}else if (subcmd.equals("maizhuang")) {
						tbl.Handle_onMaiZhuang(lUid, vecArg2);
					} else if (subcmd.equals("robot")) {
						tbl.Handle_onRobot(lUid, vecArg2);
					}else if (subcmd.equals("liwu")) {
						tbl.Handle_onLiWu(lUid, vecArg2);
					} else if (subcmd.equals("biaoqing")) {
						tbl.Handle_onBiaoQing(lUid, vecArg2);
					} else if (subcmd.equals("yuyin")) {
						tbl.Handle_onYuYin(lUid, vecArg2);
					} else if (subcmd.equals("chat")) {
						tbl.Handle_onChat(lUid, vecArg2);
					} else if (subcmd.equals("voice")) {
						tbl.Handle_onVoice(lUid, vecArg2);
					} else if (subcmd.equals("text")) {
						tbl.Handle_onText(lUid, vecArg2);
					} else if (subcmd.equals("mopai")) {
//						Handle_onMoPai(lUid, vecArg2);
					} else if (subcmd.equals("buhua")) {
						// Handle_onBuHua(lUid, vecArg2);
					} else if (subcmd.equals("chupai")) {
						tbl.Handle_onChuPai(lUid, vecArg2);
					} else if (subcmd.equals("long")) {
						tbl.Handle_onLongPai(lUid, vecArg2);
					} else if (subcmd.equals("pengpai")) {
						tbl.Handle_onPengPai(lUid, vecArg2);
					} else if (subcmd.equals("gangpai")) {
						tbl.Handle_onGangPai(lUid, vecArg2);
					} else if (subcmd.equals("fangzi_gang")) {
						tbl.Handle_onLongPai(lUid, vecArg2);
					} else if (subcmd.equals("fangzi_guo")) {
						tbl.Handle_onFangzi_guo(lUid, vecArg2);
					} else if (subcmd.equals("guopai")) {
						tbl.Handle_onGuoPai(lUid, vecArg2);
					} else if (subcmd.equals("hupai")) {
						tbl.Handle_onHuPai(lUid, vecArg2);
					} else if (subcmd.equals("try_dismissroom")) {
						tbl.Handle_onTryDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("agree_dismissroom")) {
						tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("refuse_dismissroom")) {
						tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
					}
				}
			}
		}
	}
}
